package com.yihu.wlyy.weixin.process;

import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.weixin.util.WeiXinMessageReplyUtils;
import com.yihu.wlyy.weixin.util.WeiXinMessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.repository.cdi.Eager;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 微信事件处理
 *
 * Created by lyr on 2016/08/11.
 */
@Component
public class WeiXinEventProcess {

    /**
     * 微信推送事件处理
     *
     * @param message
     * @return
     */
    public static String eventProcess(Map<String, String> message) throws Exception {
        String result = "";
        String event = message.get("Event");

        switch (event) {
            case WeiXinMessageUtils.EVENT_TYPE_SCAN: // 扫描二维码事件
                result = scanEventProcess(message);
                break;
            case WeiXinMessageUtils.EVENT_TYPE_SUBSCRIBE: // 订阅事件
                if (message.containsKey("EventKey") && StringUtils.isNotEmpty(message.get("EventKey")) && message.containsKey("Ticket")
                        && StringUtils.isNotEmpty(message.get("Ticket"))) {
                    result = scanEventProcess(message);
                }else{
                    result = subscribeEventProcess(message);
                }
                break;
            default:
                break;
        }

        return result;
    }

    /**
     * 关注事件消息发送
     *
     * @param message
     * @return
     * @throws Exception
     */
    private static String subscribeEventProcess(Map<String,String> message) throws Exception{
        String result = "";
        // 配置信息
        Properties systemConf = SystemConf.getInstance().getSystemProperties();
        // 图文信息
        List<Map<String,String>> articles =  new ArrayList<>();
        Map<String,String> article = new HashMap<>();
        // 图文URL
        String url = systemConf.getProperty("doctor_subscribe_url");
        // 图文消息图片URL
        String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
        // URL设置服务器URL、AppId
        url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
                .replace("{appId}", systemConf.getProperty("appId"));
        //图片地址
        picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));

        article.put("Url",url);
        article.put("Title", "欢迎关注厦门i健康，快来签约家庭医生吧~");
        article.put("Description","请点击查看家庭签约");
        article.put("PicUrl",picUrl);

        articles.add(article);
        // 构建回复消息XML
        result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);

        return result;
    }

    /**
     * 微信扫码事件处理
     *
     * @param message
     * @return
     * @throws Exception
     */
    private static String scanEventProcess(Map<String, String> message) throws Exception {
        String result = "";
        // 场景值
        String eventKey = message.get("EventKey");

        if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("qr_") ||
                eventKey.startsWith("qrscene_qr_"))) {
            // 配置
            Properties systemConf = SystemConf.getInstance().getSystemProperties();
            // 图文信息
            List<Map<String,String>> articles =  new ArrayList<>();
            Map<String,String> article = new HashMap<>();
            // 二维码跳转URL
            String url = systemConf.getProperty("doctor_qrcode_url");
            // 图片URL
            String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
            // 二维码参数
            String[] keys = eventKey.replace("qrscene_qr_","").replace("qr_","").split("_");

            // 图文消息URL
            url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
                    .replace("{appId}", systemConf.getProperty("appId"))
                    .replace("{doctorCode}",keys[0]);
            //图片地址
            picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));

            article.put("Url",url);
            article.put("Title",keys[1] + "医生签约");
            article.put("Description","请点击查看医生详情并申请签约");
            article.put("PicUrl",picUrl);

            articles.add(article);
            // 消息XML
            result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
        }else if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("hs_") ||
                eventKey.startsWith("qrscene_hs_"))){
            // 配置信息
            Properties systemConf = SystemConf.getInstance().getSystemProperties();
            // 图文信息
            List<Map<String,String>> articles =  new ArrayList<>();
            Map<String,String> article = new HashMap<>();
            // 社区二维码跳转URL
            String url = systemConf.getProperty("doctor_hos_qrcode_url");
            // 图片URL
            String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
            // 二维码参数
            String[] keys = eventKey.replace("qrscene_hs_","").replace("hs_","").split("_");

            // 图文消息URL
            url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
                    .replace("{appId}", systemConf.getProperty("appId"))
                    .replace("{hospital}",keys[0] + ":" + keys[1]);
            //图片地址
            picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));

            article.put("Url",url);
            article.put("Title",keys[1]);
            article.put("Description","请点击查看社区详情");
            article.put("PicUrl",picUrl);

            articles.add(article);
            // 消息XML
            result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
        }

        return result;
    }
}
